package com.tanhua.server.service;

import com.tanhua.common.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

/**
 * @Author 王加鑫
 * @Date 2021/09/03 19:29
 * @Since JDK 1.8
 */
@Service
@Slf4j
public class UserService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${tanhua.sso.url}")
    private String ssoUrl;

    /**
     * 通过sso的rest接口查询
     */
    public User queryUserByToken(String token){
        String url = ssoUrl + "/user/" + token;
        User user = null;
        //使用restTemplate+url就可以跨服务器访问资源
        try {
            user = this.restTemplate.getForObject(url, User.class);
            if(null == user){
                return null;
            }
        } catch (RestClientException e) {
            log.error("获取token失败，token =" + token,e);
        }
        return user;
    }
}
